package ServerOperation;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import javax.swing.ImageIcon;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.itextpdf.awt.DefaultFontMapper;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

import Boundry.Splash;
import Controllers.GenericToolsForController.PDFWriter;



public class ReportPDFWriter extends PDFWriter
{
	private Document document;
	private PdfWriter pdfWriter;
	private String fileName;
	
    private  Font regualrFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    private  Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED);
	private  Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
	private  Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
	private  Font bigTitleFont = new Font(Font.FontFamily.TIMES_ROMAN, 20, Font.BOLD);
	private  Font underlineTitleFont = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.UNDERLINE);
	private Paragraph currentParagraph;

	
	public ReportPDFWriter (String filename)
	{
		pdfWriter = null;
		 document = new Document();		 fileName = new String(filename);
		try {
			pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(filename));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		document.open();
		addMetaData();
	}
	    
	public void write(String strline)
	{
		try {
			document.add(new Paragraph(strline,regualrFont));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
	}
	
	public void writeTitle(String strline)
	{
		try {
			document.add(new Paragraph(strline,bigTitleFont));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
	}
	
	
	
	public void writeUnderline(String strline)
	{
		try {
			document.add(new Paragraph(strline,underlineTitleFont));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
	}
	
	public void newPage()
	{
		document.newPage();
	}
	public void write(JFreeChart chart, int width, int height, int x, int y)
	{
		{
			PdfContentByte contentByte = pdfWriter.getDirectContent();
			PdfTemplate template = contentByte.createTemplate(width, height);
			
			Graphics2D graphics2d = template.createGraphics (width, height);
			Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,height);
			chart.draw(graphics2d, rectangle2d);
			graphics2d.dispose();
					//add image
			contentByte.addTemplate(template, x, y);
		} 
	}
		public void write(JFreeChart chart, int width, int height)
		{
	            BufferedImage bufferedImage = chart.createBufferedImage(width, height);
	            Image image = null;
				try {
					image = Image.getInstance(pdfWriter, bufferedImage, 1.0f);
				} catch (BadElementException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
	            try {
					document.add(image);
				} catch (DocumentException e) {
					e.printStackTrace();
				}
	             
			} 
		
		public void write(Image image) 
		{
			try {
				document.add(image);
			} catch (DocumentException e) {
				e.printStackTrace();
			}
  
				}
		
		public void write(Integer integer) 
		{
			write(integer.toString());
				}
	         
	             
	   private  void addMetaData() {
	          document.addTitle("Automatically Generated Statistical Report");
	          document.addSubject("Statistical Report");
	          document.addAuthor("ICM System Automate generation");
	          document.addCreator("ICM System Automate generation");
	        }
	 public  void addEmptyLine(int number) {
         for (int i = 0; i < number; i++) {
        	 write(" ");
         }
       }

	public void close()
	{
		document.close();
		
	}
	
	public void addLogo()
	{
	try {
	
        Image image1 = Image.getInstance(Splash.class.getResource("/ICONS/logo.png"));
        document.add(image1);
    } catch(Exception e){
      e.printStackTrace();
    }
  }
	
	public enum ChartType 
	{
	PIECHART(0), CARCHART(1), XYGRAPH(2);
	
	
	private int var;
	private ChartType (int val)
	{
		var = val;
	}
	
	
	}
	
		
	
	
	public JFreeChart createPieChartForReport(Map<String, Float> pieMap, String title) 
	{
		    DefaultPieDataset dataSet = new DefaultPieDataset();
		    for (String key : pieMap.keySet()) 
		    {
		    dataSet.setValue(key,pieMap.get(key));	
		    }
		    JFreeChart chart = ChartFactory.createPieChart(
		    		title, dataSet, true, true, false);
		    return chart;
	}

	

	

    public  JFreeChart createBarChartForReport(Map<String, Float> destriMap2, String title) {
        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        for( String str : destriMap2.keySet())
        {
            dataSet.setValue(destriMap2.get(str), "Requests", str);

        	
        }
        
      
        JFreeChart chart = ChartFactory.createBarChart(
                title, "Work days", "Num of Requests",
                dataSet, PlotOrientation.VERTICAL, true, true, true);
 
        return chart;
    }
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	public static void main(String[] args) 
		{
		   ReportPDFWriter testFile = new ReportPDFWriter("test.PDF");
		   testFile.writeTitle("This is testing report\n");
		   testFile.writeUnderline("Look at this chart: \n");
		   testFile.writeUnderline("test1");
		   testFile.writeUnderline("test2");
		  // testFile.write(generateBarChart(), 500, 400);
		  // testFile.newPage();
		 //  testFile.write(generateBarChart(), 500, 400, 0,0);
		   testFile.close();
			
		}
		public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) 
		{
			PdfWriter writer = null;
			Document document = new Document();
			try 
			{
				writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
				document.open();
				document.add(new Paragraph("pie chart"));
				PdfContentByte contentByte = writer.getDirectContent();
				PdfTemplate template = contentByte.createTemplate(width, height);
				Graphics2D graphics2d = template.createGraphics(width, height,new DefaultFontMapper());
				Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,height);
				chart.draw(graphics2d, rectangle2d);
				graphics2d.dispose();
				contentByte.addTemplate(template, 0, 400);
			} 
			catch (Exception e) 
			{
			e.printStackTrace();
			}
			document.close();
			}
	 
	  	
/*
public static JFreeChart generateBarChart() {
    DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
    dataSet.setValue(791, "Population", "1750 AD");
    dataSet.setValue(978, "Population", "1800 AD");
    dataSet.setValue(1262, "Population", "1850 AD");
    dataSet.setValue(1650, "Population", "1900 AD");
    dataSet.setValue(2519, "Population", "1950 AD");
    dataSet.setValue(6070, "Population", "2000 AD");

    JFreeChart chart = ChartFactory.createBarChart(
            "World Population growth", "Year", "Population in millions",
            dataSet, PlotOrientation.VERTICAL, false, true, false);
	return chart;

}
*/
}

	

